package com.dyh.quickdemozwei.fieldsDef;

import com.alibaba.fastjson.JSONObject;
import com.dyh.quickdemozwei.domain.Foo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.stereotype.Service;
import org.springframework.util.NumberUtils;

import java.util.Collection;
import java.util.Map;

/**
 * @Author: yhdong
 * @Date: 2021/9/8 19:36
 */
@Service
public class FieldVisitorService {

    @Autowired
    ConversionService conversionService;
    //DefaultConversionService;

//    public void fun(){
////
//        String srt = "1";
//        Integer res = conversionService.convert(srt,Integer.class);
//        System.out.println();
//    }
    @Getter@Setter
    public static class  Config{
        Map<String,FieldVisitor> upMap;
        Map<String,FieldVisitor> downMap;
        Map<String,String> links;
    }

    public void mapping(Object up,Object down,Config config){
        Map<String,FieldVisitor> upMap = config.getUpMap();
        Map<String,FieldVisitor> downMap = config.getDownMap();
        Map<String,String> links = config.getLinks();
        for (Map.Entry<String, FieldVisitor> entry : upMap.entrySet()) {
            String anthorKey = links.get(entry.getKey());
            FieldVisitor downVisitor = downMap.get(anthorKey);
            if (downVisitor!=null){
                FieldVisitor upVisitor = entry.getValue();
                Object val = upVisitor.getGetter().apply(up);
                if (upVisitor.getFiledClass()==downVisitor.getFiledClass()){
                    downVisitor.getSetter().accept(down,val);
                }
                else {
                    if (Collection.class.isAssignableFrom(upVisitor.getFiledClass())||
                        Map.class.isAssignableFrom(upVisitor.getFiledClass())
                    ){
                        downVisitor.getSetter().accept(down,val);
                    }
                    else {
                        Object convertedVal = conversionService.convert(val,downVisitor.getFiledClass());
                        downVisitor.getSetter().accept(down,convertedVal);
                    }


                }
            }
        }
    }

//    public  void mapping(JSONObject jo, Map<String,FieldVisitor> joVisitorMap, Foo foo, Map<String,FieldVisitor> fooVisitorMap){
//        for (Map.Entry<String, FieldVisitor> entry : joVisitorMap.entrySet()) {
//            FieldVisitor visitor2 = fooVisitorMap.get(entry.getKey());
//            if (visitor2!=null){
//                FieldVisitor visitor1 = entry.getValue();
//                Object val = visitor1.getter.apply(jo);
//                if (visitor1.filedClass==visitor2.filedClass){
//                    visitor2.setter.accept(foo,val);
//                }
//                else {
//                    Object convertedVal = conversionService.convert(val,visitor2.filedClass);
//                    visitor2.setter.accept(foo,convertedVal);
////                    if (val instanceof Number){
////                        visitor2.setter.accept(foo, NumberUtils.convertNumberToTargetClass((Number) val,visitor2.filedClass));
////                    }
////                    if ()
//                }
//            }
//        }
//    }
}
